{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "dc9c5b69",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   subject_id                 hr                hrv\n",
      "0  subject_id                 hr                hrv\n",
      "1           1   95.5796963146767  66.05244183646909\n",
      "2           2  60.89832200997166  90.73874228553221\n",
      "3           3  82.39808319417534  91.91497425646716\n",
      "4           4  76.91819669509869  64.99894357948662\n"
     ]
    }
   ],
   "source": [
    "#loading of data \n",
    "\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "master_df = pd.read_csv(r'C:\\Users\\DELL\\Downloads\\master_data (1).csv' , header = None)\n",
    "columns = ['subject_id','hr','hrv']\n",
    "records = master_df.to_numpy().tolist()\n",
    "master_df = pd.DataFrame(records, columns=columns)\n",
    "print(master_df.head())     "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "a3c021d4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                      0                       1           2\n",
      "0                  time              ppg_signal  subject_id\n",
      "1                   0.0  -0.0051624524397875255           1\n",
      "2  0.010001666944490749     0.11550336263698832           1\n",
      "3  0.020003333888981498     0.23982087440920388           1\n",
      "4  0.030005000833472247     0.33952906249938297           1\n",
      "                      0                       1           2\n",
      "0                  time              ppg_signal  subject_id\n",
      "1                   0.0  0.00032218383652521447           2\n",
      "2  0.010001666944490749     0.07227414383878186           2\n",
      "3  0.020003333888981498     0.16194613325557097           2\n",
      "4  0.030005000833472247      0.2311690454256863           2\n",
      "                      0                      1           2\n",
      "0                  time             ppg_signal  subject_id\n",
      "1                   0.0  -0.017260945440781654           3\n",
      "2  0.010001666944490749    0.09377250665151135           3\n",
      "3  0.020003333888981498    0.20084672243142476           3\n",
      "4  0.030005000833472247     0.3062963462814766           3\n",
      "                      0                      1           2\n",
      "0                  time             ppg_signal  subject_id\n",
      "1                   0.0  0.0014665488492327373           4\n",
      "2  0.010001666944490749    0.09064218924392801           4\n",
      "3  0.020003333888981498    0.17913963566669158           4\n",
      "4  0.030005000833472247     0.2684621568766273           4\n",
      "                      0                      1           2\n",
      "0                  time             ppg_signal  subject_id\n",
      "1                   0.0  -0.007629165420233813           5\n",
      "2  0.010001666944490749    0.13386080930531857           5\n",
      "3  0.020003333888981498     0.1988315376691297           5\n",
      "4  0.030005000833472247    0.30564478830105324           5\n",
      "                      0                      1           2\n",
      "0                  time             ppg_signal  subject_id\n",
      "1                   0.0  -0.004676773442691768           6\n",
      "2  0.010001666944490749    0.10432745958442287           6\n",
      "3  0.020003333888981498    0.22102944947855108           6\n",
      "4  0.030005000833472247     0.3417773225980092           6\n",
      "                      0                      1           2\n",
      "0                  time             ppg_signal  subject_id\n",
      "1                   0.0  0.0015683793897368207           7\n",
      "2  0.010001666944490749    0.07363157894416314           7\n",
      "3  0.020003333888981498    0.17246728137066303           7\n",
      "4  0.030005000833472247    0.24283825803583756           7\n",
      "                      0                     1           2\n",
      "0                  time            ppg_signal  subject_id\n",
      "1                   0.0  0.006561548684092093           8\n",
      "2  0.010001666944490749    0.0615373929708707           8\n",
      "3  0.020003333888981498    0.1712725197779946           8\n",
      "4  0.030005000833472247    0.2383331677118505           8\n",
      "                      0                      1           2\n",
      "0                  time             ppg_signal  subject_id\n",
      "1                   0.0  -0.016366525257603196           9\n",
      "2  0.010001666944490749     0.0833586225820312           9\n",
      "3  0.020003333888981498     0.1817678699938386           9\n",
      "4  0.030005000833472247     0.2623295613620525           9\n",
      "                      0                    1           2\n",
      "0                  time           ppg_signal  subject_id\n",
      "1                   0.0  0.00418943616721943          10\n",
      "2  0.010001666944490749  0.09610774440086996          10\n",
      "3  0.020003333888981498  0.19981370718958455          10\n",
      "4  0.030005000833472247   0.2826484436165122          10\n",
      "                      0                     1           2\n",
      "0                  time            ppg_signal  subject_id\n",
      "1                   0.0  0.001795865591929432          11\n",
      "2  0.010001666944490749     0.071021713227337          11\n",
      "3  0.020003333888981498   0.18132717770284146          11\n",
      "4  0.030005000833472247   0.24634891178745233          11\n",
      "                      0                     1           2\n",
      "0                  time            ppg_signal  subject_id\n",
      "1                   0.0  0.004561543193092921          12\n",
      "2  0.010001666944490749   0.11643971274879791          12\n",
      "3  0.020003333888981498    0.2240996258685957          12\n",
      "4  0.030005000833472247   0.29951674781872717          12\n",
      "                      0                      1           2\n",
      "0                  time             ppg_signal  subject_id\n",
      "1                   0.0  -0.024995864218494684          13\n",
      "2  0.010001666944490749    0.11704973725277797          13\n",
      "3  0.020003333888981498     0.2234046195925888          13\n",
      "4  0.030005000833472247     0.3444551814038143          13\n",
      "                      0                      1           2\n",
      "0                  time             ppg_signal  subject_id\n",
      "1                   0.0  -0.006505704843570877          14\n",
      "2  0.010001666944490749    0.10723027934827362          14\n",
      "3  0.020003333888981498    0.22230287738001675          14\n",
      "4  0.030005000833472247    0.33178463650242446          14\n",
      "                      0                      1           2\n",
      "0                  time             ppg_signal  subject_id\n",
      "1                   0.0  -0.015861233412454222          15\n",
      "2  0.010001666944490749    0.11515196408083003          15\n",
      "3  0.020003333888981498    0.20230443524312128          15\n",
      "4  0.030005000833472247    0.32858422895646017          15\n",
      "                      0                     1           2\n",
      "0                  time            ppg_signal  subject_id\n",
      "1                   0.0  0.001873667558525646          16\n",
      "2  0.010001666944490749     0.102974243343387          16\n",
      "3  0.020003333888981498   0.21069925300992318          16\n",
      "4  0.030005000833472247    0.2988368733996929          16\n",
      "                      0                       1           2\n",
      "0                  time              ppg_signal  subject_id\n",
      "1                   0.0  -0.0030311407513644085          17\n",
      "2  0.010001666944490749     0.11259824888184886          17\n",
      "3  0.020003333888981498     0.20187517155740978          17\n",
      "4  0.030005000833472247     0.29306081243816806          17\n",
      "                      0                     1           2\n",
      "0                  time            ppg_signal  subject_id\n",
      "1                   0.0  -0.00763782102686241          18\n",
      "2  0.010001666944490749   0.08186088674233531          18\n",
      "3  0.020003333888981498   0.14555897338086174          18\n",
      "4  0.030005000833472247    0.2126929254266242          18\n",
      "                      0                      1           2\n",
      "0                  time             ppg_signal  subject_id\n",
      "1                   0.0  -0.016812530168527216          19\n",
      "2  0.010001666944490749    0.09573018395806032          19\n",
      "3  0.020003333888981498    0.21913093028543443          19\n",
      "4  0.030005000833472247    0.29405222873365355          19\n",
      "                      0                      1           2\n",
      "0                  time             ppg_signal  subject_id\n",
      "1                   0.0  -0.015288130492334183          20\n",
      "2  0.010001666944490749    0.08293137143821008          20\n",
      "3  0.020003333888981498     0.1892882524708765          20\n",
      "4  0.030005000833472247    0.27829099877503405          20\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "#loadind subjects\n",
    "\n",
    "sub_1 = pd.read_csv(r'C:\\Users\\DELL\\Downloads\\subject_1.csv', header=None)\n",
    "print(sub_1.head())\n",
    "\n",
    "\n",
    "sub_2 = pd.read_csv(r'C:\\Users\\DELL\\Downloads\\subject_2.csv', header=None)\n",
    "print(sub_2.head())\n",
    "\n",
    "sub_3 = pd.read_csv(r'C:\\Users\\DELL\\Downloads\\subject_3.csv', header=None)\n",
    "print(sub_3.head())\n",
    "\n",
    "sub_4 = pd.read_csv(r'C:\\Users\\DELL\\Downloads\\subject_4.csv', header=None)\n",
    "print(sub_4.head())\n",
    "\n",
    "sub_5 = pd.read_csv(r'C:\\Users\\DELL\\Downloads\\subject_5.csv', header=None)\n",
    "print(sub_5.head())\n",
    "\n",
    "sub_6 = pd.read_csv(r'C:\\Users\\DELL\\Downloads\\subject_6.csv', header=None)\n",
    "print(sub_6.head())\n",
    "\n",
    "sub_7 = pd.read_csv(r'C:\\Users\\DELL\\Downloads\\subject_7.csv', header=None)\n",
    "print(sub_7.head())\n",
    "\n",
    "sub_8 = pd.read_csv(r'C:\\Users\\DELL\\Downloads\\subject_8.csv', header=None)\n",
    "print(sub_8.head())\n",
    "\n",
    "sub_9 = pd.read_csv(r'C:\\Users\\DELL\\Downloads\\subject_9.csv', header=None)\n",
    "print(sub_9.head())\n",
    "\n",
    "sub_10 = pd.read_csv(r'C:\\Users\\DELL\\Downloads\\subject_10.csv', header=None)\n",
    "print(sub_10.head())\n",
    "\n",
    "sub_11 = pd.read_csv(r'C:\\Users\\DELL\\Downloads\\subject_11.csv', header=None)\n",
    "print(sub_11.head())\n",
    "\n",
    "sub_12 = pd.read_csv(r'C:\\Users\\DELL\\Downloads\\subject_12.csv', header=None)\n",
    "print(sub_12.head())\n",
    "\n",
    "sub_13 = pd.read_csv(r'C:\\Users\\DELL\\Downloads\\subject_13.csv', header=None)\n",
    "print(sub_13.head())\n",
    "\n",
    "sub_14 = pd.read_csv(r'C:\\Users\\DELL\\Downloads\\subject_14.csv', header=None)\n",
    "print(sub_14.head())\n",
    "\n",
    "sub_15 = pd.read_csv(r'C:\\Users\\DELL\\Downloads\\subject_15.csv', header=None)\n",
    "print(sub_15.head())\n",
    "\n",
    "sub_16 = pd.read_csv(r'C:\\Users\\DELL\\Downloads\\subject_16.csv', header=None)\n",
    "print(sub_16.head())\n",
    "\n",
    "sub_17 = pd.read_csv(r'C:\\Users\\DELL\\Downloads\\subject_17.csv', header=None)\n",
    "print(sub_17.head())\n",
    "\n",
    "sub_18 = pd.read_csv(r'C:\\Users\\DELL\\Downloads\\subject_18.csv', header=None)\n",
    "print(sub_18.head())\n",
    "\n",
    "sub_19 = pd.read_csv(r'C:\\Users\\DELL\\Downloads\\subject_19.csv', header=None)\n",
    "print(sub_19.head())\n",
    "\n",
    "sub_20 = pd.read_csv(r'C:\\Users\\DELL\\Downloads\\subject_20.csv', header=None)\n",
    "print(sub_20.head())\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ba8bbcb9",
   "metadata": {},
   "outputs": [],
   "source": [
    "from scipy.stats import skew, kurtosis\n",
    "from scipy.signal import find_peaks\n",
    "\n",
    "def extract_features(df, fs=100):\n",
    "    signal = df['ppg_filtered'].values\n",
    "    time = df['time'].values\n",
    "    subject_id = df['subject'].iloc[0] if 'subject' in df.columns else None\n",
    "\n",
    "    peaks, _ = find_peaks(signal, distance=int(fs * 0.6))\n",
    "\n",
    "    rr_intervals = np.diff(time[peaks])\n",
    "\n",
    "    features = {\n",
    "        \"subject\": subject_id,\n",
    "        \"peak_count\": len(peaks),\n",
    "        \"mean_rr\": np.mean(rr_intervals) if len(rr_intervals) > 0 else np.nan,\n",
    "        \"std_rr\": np.std(rr_intervals) if len(rr_intervals) > 0 else np.nan,\n",
    "        \"min_rr\": np.min(rr_intervals) if len(rr_intervals) > 0 else np.nan,\n",
    "        \"max_rr\": np.max(rr_intervals) if len(rr_intervals) > 0 else np.nan,\n",
    "        \"skewness\": skew(rr_intervals) if len(rr_intervals) > 0 else np.nan,\n",
    "        \"kurtosis\": kurtosis(rr_intervals) if len(rr_intervals) > 0 else np.nan\n",
    "    }\n",
    "\n",
    "    return features\n",
    "\n",
    "\n",
    " \n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
